// 引入依赖
import { Command } from 'commander';
// 引入提示颜色
import colors from 'colors';
import axios from 'axios';
import { getCityCode } from './cityInfo';

const program = new Command();

// 定义版本和参数选项
program.version('0.1.0', '-v, --version').option('-i, --init', 'init something').option('-c, --city [name]', 'Add city name').parse(process.argv);

let noCommand = process.argv.slice(2).length == 0;
if (noCommand) {
    program.outputHelp(colors.green);
    process.exit();
}

// 网络
// 定义接口
interface WeatherRes {
    status: string;
    count: string;
    info: string;
    infocode: string;
    lives: Live[];
}

interface Live {
    province: string;
    city: string;
    adcode: string;
    weather: string;
    temperature: string;
    winddirection: string;
    windpower: string;
    humidity: string;
    reporttime: string;
}

// console.log(program.opts());

const baseURL = 'https://restapi.amap.com/v3/weather/weatherInfo';
const key = 'e0ebe953599c22bb7abaa746c17d0a69';
let cityCode = getCityCode(program.opts().city);

if (cityCode) {
    axios
        .get(`${baseURL}?key=${key}&city=${cityCode}`)
        .then(res => {
            // console.log(res.data);
            const live = res.data.lives[0];
            console.log(colors.yellow(live.reporttime));
            console.log(colors.white(live.province + ' ' + live.city));
            console.log(colors.green(`${live.weather} ${live.temperature} 度`));
        })
        .catch(err => {
            console.log(colors.red('天气服务出现异常'));
        });
} else {
    console.log(colors.red('城市暂未信息'));
}
